import java.util.LinkedList;
import java.util.List;
import java.util.PriorityQueue;

class MedianFinder {
    PriorityQueue<Integer> minQueue;
    PriorityQueue<Integer> maxQueue;

    public MedianFinder() {
        // 从对头递减排序
        minQueue = new PriorityQueue<>((a, b) -> b - a);

        // 从对头递增排序
        maxQueue = new PriorityQueue<>((a, b) -> a - b);
    }

    public void addNum(int num) {
        if (minQueue.isEmpty() || minQueue.peek() >= num) {
            minQueue.offer(num);

            // 始终保持minQueue始终比maxQueue多一个元素
            if (minQueue.size() > maxQueue.size() + 1) {
                maxQueue.offer(minQueue.poll());
            }
        } else {
            maxQueue.offer(num);

            // 始终保持minQueue始终比maxQueue多一个元素
            if (maxQueue.size() > minQueue.size()) {
                minQueue.offer(maxQueue.poll());
            }
        }
    }

    public double findMedian() {
        if (minQueue.size() > maxQueue.size()) {
            // 自动拆箱，自动类型转换
            return minQueue.peek();
        }
        return (minQueue.peek() + maxQueue.peek()) / 2.0;
    }
}

public class Solution295 {
    public static void main(String[] args) {
        MedianFinder medianFinder = new MedianFinder();
        medianFinder.addNum(1);
        medianFinder.addNum(2);
        System.out.println(medianFinder.findMedian());
        medianFinder.addNum(3);
        System.out.println(medianFinder.findMedian());
    }
}


/**
 * Your MedianFinder object will be instantiated and called as such:
 * MedianFinder obj = new MedianFinder();
 * obj.addNum(num);
 * double param_2 = obj.findMedian();
 */
